package
{
	import com.compton.EVENT.NavigationEvent;
	import com.compton.EVENT.WineEvent;
	import com.compton.UI.ControlPanel;
	import com.compton.UI.Favorites;
	import com.compton.UI.Search;
	import com.compton.UI.UpdateApplication;
	import com.compton.UI.WineServices;
	import com.compton.VO.SearchVO;
	
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.ContextMenuEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	
	import libs.CloseButton;
	import libs.MinButton;
	
	[SWF(width = "425", height = "500", frameRate = "40")]
	
	public class Main extends Sprite
	{
		private var _favoritePanel:Favorites;
		private var _wineSVC:WineServices;
		private var _control:ControlPanel;
		private var _search:Search;
		private var _container:Sprite;
		private var _searchResults:SearchVO;
		
		public function Main()
		{
			firstAssemble();
			stage.scaleMode = StageScaleMode.EXACT_FIT;
			_wineSVC.addEventListener(WineEvent.WINE_SEARCH_ERROR, onWineError);
			_wineSVC.addEventListener(WineEvent.ADD_WINES, onWinesAdded);
		}
		
		//first assemble will build the container for the screens and creat the first panel and control panel
		private function firstAssemble():void
		{	
			_container = new Sprite();
			this.addChild(_container);
			
			_favoritePanel = new Favorites();
			_container.addChild(_favoritePanel);
			
			_control = new ControlPanel();
			_container.addChildAt(_control,1);
			_control.x = 50;
			_control.y = 422;
			
			_control.addEventListener(NavigationEvent.GO2_SEARCH, go2Favorite);
			_control.addEventListener(NavigationEvent.GO2_FAVORITE, go2Search);
			
			var updateApp:UpdateApplication = new UpdateApplication();	
			
			var closeBtn:CloseButton = new CloseButton();
			addChild(closeBtn);
			closeBtn.x = stage.stageWidth - 30;
			closeBtn.y = 5;
			closeBtn.buttonMode = true;
			closeBtn.addEventListener(MouseEvent.CLICK, onClose);
			
			var minBtn:MinButton = new MinButton();
			addChild(minBtn);
			minBtn.x = closeBtn.x - 25;
			minBtn.y = closeBtn.y;
			minBtn.buttonMode = true
			minBtn.addEventListener(MouseEvent.CLICK, onMin);
			
			addEventListener(Event.ADDED_TO_STAGE, copyright);
			_wineSVC = new WineServices();
			loadData();
		}
		
		//Closes the custom chrome
		private function onClose(evt:MouseEvent):void
		{
			stage.nativeWindow.close();
		}
		
		//Minimizes the custom chrome
		private function onMin(evt:MouseEvent):void
		{
			stage.nativeWindow.minimize();
		}
		
		//this event listener will listen for the click of the search button and change the screen accordingly
		private function go2Search(evt:NavigationEvent):void
		{
			if(_container.numChildren > 1)
			{
				_container.removeChildAt(0);
				_search = new Search(_searchResults);
				_container.addChildAt(_search, 0);
				_search.addEventListener(WineEvent.START_SEARCH, onSearch);
			}
		}
		
		//the search panel will also listen for the search button press and start loading the xml for the new search
		//fix so that it is just one object
		private function onSearch(evt:WineEvent):void
		{
			_wineSVC.recieveXML(evt.searchVO.type,"50",evt.searchVO.minPrice,evt.searchVO.maxPrice,evt.searchVO.country);
			_searchResults = evt.searchVO;
			trace(evt.searchVO.minPrice);
		}
		
		//this event listener will listen for the favorites screen press and switch to that panel accordingly
		private function go2Favorite(evt:NavigationEvent):void
		{
			if(_container.numChildren > 1)
			{
				_container.removeChildAt(0);
				_container.addChildAt(_favoritePanel, 0);
				loadData();
			}
		}
		
		//The load Data function starts loading the data that needs to be passed into the wine services class
		private function loadData():void
		{
			//_wineSVC = new WineServices();
			var vo:SearchVO = new SearchVO();
			vo.type = "red wine";
			vo.country = "US";
			//vo.zip = "32707";
			vo.maxPrice = "1000";
			vo.minPrice = "0";
			
			
			_wineSVC.recieveXML(vo.type, "50", vo.minPrice, vo.maxPrice, vo.country);
			_wineSVC.addEventListener(WineEvent.ADD_WINES, onWinesAdded);
		}
		
		//onError of loading and parsing of XML Data the search panel will provide a response
		private function onWineError(evt:WineEvent):void
		{
			if(_search)
			{
				_search.tfError.text = "Plaease Try Arain!";		
			}	
		}
		
		//after the data has been loaded it will transfer the data to the favorites panel and start placing the data
		private function onWinesAdded(evt:WineEvent):void
		{	
			_favoritePanel.wineArray = evt.wineArray;
			
			if(_search && _container.contains(_search))
			{
				trace('a');
				_container.removeChild(_search);
				_container.addChildAt(_favoritePanel, 0);
				_favoritePanel.tfHeader.text = "Wine Results";
				_favoritePanel.tfHeader.mouseEnabled = false;
			}else{
				_favoritePanel.tfHeader.text = "Wine Cellar";
				_favoritePanel.tfHeader.mouseEnabled = false;
			}
		}
		
		//adds the copright information on the right click of the mouse
		private function copyright(evt:Event):void
		{
			var cm:ContextMenu = new ContextMenu();
			cm.hideBuiltInItems();
			
			var items:ContextMenuItem = new ContextMenuItem("©2011 FlightCode", true);
			items.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, copyrightClick);
			cm.customItems.push(items);
			this.contextMenu = cm;
		}
		
		//provides the URL to Snooth website for more information
		private function copyrightClick(evt:ContextMenuEvent):void
		{
			navigateToURL(new URLRequest("http://www.snooth.com"));
		}
	}
}